package com.annoncesautomobile.modele.util;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.os.AsyncTask;
import android.util.Log;
class XMLDowloader 
extends AsyncTask<String, Integer, Document>{
	protected void onPreExecute(){
		//Setup is done here
	}
	@Override
	protected Document doInBackground(String... params) {
		try{
			//URL url = new URL(params[0]);
			HttpGet uri = new HttpGet(params[0]);    
			DefaultHttpClient client = new DefaultHttpClient();
			HttpResponse resp = null;
			Document doc = null;
			resp = client.execute(uri);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;		
			builder = factory.newDocumentBuilder(); 
			doc = builder.parse(resp.getEntity().getContent());
			return doc;
		} catch (IllegalStateException e) {
			Log.e("XML","Failed to load xml - IllegalState",e);
		} catch (ClientProtocolException e) {
			Log.e("XML","Failed to load xml - ClientProtocole",e);
		}catch (IOException e) {
			Log.e("XML","Failed to load xml - IO",e);
		}	
		catch (ParserConfigurationException e) {
			Log.e("XML","Failed to load xml - ParserConfiguration",e);
		}
		catch (SAXException e) {
			Log.e("XML","Failed to load xml - SAX",e);
		}
		return null;
	} 
	protected void onProgressUpdate(Integer... params){
		//Update a progress bar here, or ignore it, it's up to you
	}
	/*
	protected void onPostExecute(Document doc){
		ImageView iv = (ImageView)findViewById(R.id.remote_image);
		if(iv!=null && img!=null){
			iv.setImageBitmap(img);
		}
	}
	*/
	protected void onCancelled(){
	}
}